package com.example.mall.controller;

import com.alibaba.fastjson.JSON;
import com.example.mall.pojo.*;
import com.example.mall.service.SpRoleService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@AllArgsConstructor
public class RoleController {

    SpRoleService spRoleService;

    @DeleteMapping("/roles/{id}")
    public Rbody deleteRolesById(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();
        Boolean deleteRoles = spRoleService.deleteRoles(id);
        if(!deleteRoles){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("删除失败");
        }

        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("删除成功");
        return rbody;
    }

    @PutMapping("/roles/{id}")
    public Rbody updateRolesById(@PathVariable("id") Integer id,@RequestBody String body){
        Rbody rbody = new Rbody();
        System.out.println(body);
        SpRole spRole = JSON.parseObject(body, SpRole.class);
        Boolean update = spRoleService.updateRoles(spRole.getRole_name(), spRole.getRole_desc(), id);
        if(!update){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("更新失败");
        }

        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("更新成功");
        return rbody;
    }


    @GetMapping("/roles/{id}")
    public Rbody getRolesById(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();

        NewRole1 roleById = spRoleService.getRoleById(id);
        if(roleById==null){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("获取失败");
        }
        rbody.setData(roleById);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("获取成功");
        return rbody;
    }

        @PostMapping("/roles")
    public Rbody addRoles(@RequestBody String body){
        Rbody rbody = new Rbody();
        System.out.println("body++++++"+body);
        NewRole newRole = JSON.parseObject(body, NewRole.class);
        Boolean addRoles = spRoleService.addRoles(newRole.getRoleName(), newRole.getRoleDesc());
        if(!addRoles){
            rbody.getMeta().setMsg("增加失败");
            rbody.getMeta().setStatus(400);
        }
        rbody.getMeta().setMsg("增加成功");
        rbody.getMeta().setStatus(201);
        return rbody;
    }


    @GetMapping("/roles")
    public Rbody getrolesList(){
        Rbody rbody = new Rbody();

        List<SpRole> spRoleList1 = spRoleService.getSpRoleList1();
//        List<Role> allRole = spRoleService.getAllRole();
        List<Role> roles = new ArrayList<>();

        for(SpRole spRole:spRoleList1) {
            Role role1 = new Role();
            Role1 role2 = new Role1();
            System.out.println("spRole"+spRole);
            role1.setId((int) spRole.getRole_id());
            role1.setRoleName(spRole.getRole_name());
            role1.setRoleDesc(spRole.getRole_desc());
            List<Integer> arrayList = new ArrayList<>();
            String ps_ids = spRole.getPs_ids();
            String[] split = ps_ids.split(",");
            System.out.println("!!!!!" + split);
            for (String splits : split) {
                if(splits !=null && !splits .equals("")){
                    System.out.println(splits);
                    arrayList.add(Integer.valueOf(splits));
                }

            }
            System.out.println("arrayList"+arrayList);
            List<SpPermission> permissions1 = new ArrayList<>();
            List<SpPermission> permissions2 = new ArrayList<>();
            List<SpPermission> permissions3 = new ArrayList<>();
            for (int i=0;i<arrayList.size();i++) {
                Integer integer = arrayList.get(i);
                List<SpPermission> rOle3 = spRoleService.getROle3(integer);
                System.out.println("integer" + integer);
                System.out.println("rOle3" + rOle3);
                for(SpPermission r1 :rOle3){
                    int ps_level = r1.getPs_level();
                    if(ps_level==0){
                        permissions1.add(r1);
                    }
                    if(ps_level==1){
                        permissions2.add(r1);
                    }
                    if(ps_level==2){
                        permissions3.add(r1);
                    }
                }
                System.out.println("permissions1"+permissions1);
                System.out.println("permissions2"+permissions2);
                System.out.println("permissions3"+permissions3);

                System.out.println("00000000"+permissions2.size());
                List<Role1> arrayList1 = new ArrayList<>();
                for(SpPermission r1:permissions1){
                    Role1 role11 = new Role1();
                    String psName = r1.getPs_name();
                    Short psId = r1.getPs_id();
                    String psC = r1.getPs_c();
                    role11.setPath(psC);
                    role11.setAuthName(psName);
                    role11.setId(Integer.valueOf(psId));
                    arrayList1.add(role11);



                }
                role1.setChildren(arrayList1);


                List<Role2> arrayList2 = new ArrayList<>();
                for(SpPermission r2:permissions2){
                    Role2 role22 = new Role2();
                    String psName = r2.getPs_name();
                    Short psId = r2.getPs_id();
                    String psC = r2.getPs_c();
                    role22.setPath(psC);
                    role22.setAuthName(psName);
                    role22.setId(Integer.valueOf(psId));
                    arrayList2.add(role22);

                    Short ps_id = r2.getPs_id();
                    System.out.println(ps_id);
                    List<SpPermission> spRoleList2 = spRoleService.getSpRoleList2(Integer.valueOf(ps_id));

                }
                System.out.println("arrayList2"+arrayList2);
                for(Role1 role111:arrayList1){
                    role111.setChildren(arrayList2);
                }



                List<Role3> arrayList3 = new ArrayList<>();
                for(SpPermission r3:permissions3){

                    Role3 role33 = new Role3();
                    String psName = r3.getPs_name();
                    Short psId = r3.getPs_id();
                    String psC = r3.getPs_c();
                    role33.setPath(psC);
                    role33.setAuthName(psName);
                    role33.setId(Integer.valueOf(psId));
                    arrayList3.add(role33);
                }
                System.out.println("arrayList3"+arrayList3);
                for(Role2 role222:arrayList2){
                    role222.setChildren(arrayList3);
                }

            }
            roles.add(role1);
            }

        rbody.setData(roles);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("获取角色列表成功！");
        return rbody;
    }

}
